package com.andro.inje.chansung.department;

import com.andro.inje.chansung.ConstantName;
import com.andro.inje.chansung.DB;
import com.andro.inje.chansung.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DepartmentActivity extends Activity implements OnClickListener {
	private DepartmentIntroHandler handler;
	private ProgressDialog pd;
	
	private Button phoneBtn;
	
	private TextView cheifNameTv;
	private TextView faxNumberTv;
	private TextView officeLocationTv;
	private TextView introductionTv;
	private TextView featureTv;
	private TextView learningSubjectTv;
	private TextView jobRelatedTv;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.department_intro_layout);
		handler = new DepartmentIntroHandler();
		
		phoneBtn = (Button)findViewById(R.id.department_intro_phone_btn);
		phoneBtn.setOnClickListener(this);
		
		cheifNameTv 			= (TextView)findViewById(R.id.department_intro_chief_name_tv);
		faxNumberTv 			= (TextView)findViewById(R.id.department_intro_fax_tv);
		officeLocationTv 	= (TextView)findViewById(R.id.department_intro_office_location_tv);
		introductionTv 		= (TextView)findViewById(R.id.department_intro_introduction_tv);
		featureTv 			= (TextView)findViewById(R.id.department_intro_feature_tv);
		learningSubjectTv 	= (TextView)findViewById(R.id.department_intro_learning_subject_tv);
		jobRelatedTv 		= (TextView)findViewById(R.id.department_intro_jobs_related);
		
		DepartmentIntroUpdateThread thread = new DepartmentIntroUpdateThread(this, handler);
		pd = ProgressDialog.show(this, "로딩중", "학과정보 로딩중...");
		thread.start();
	}
	
	public class DepartmentIntroHandler extends Handler {
		public void handleMessage(Message msg) {
			SQLiteDatabase db = openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
			Department department = DB.getDataFromDepartmentTable(db);
			db.close();
			
			cheifNameTv.setText(department.getCheifName());
			phoneBtn.setText(department.getPhoneNumber());
			faxNumberTv.setText(department.getFaxNumber());
			officeLocationTv.setText(department.getOfficeLocation());
			introductionTv.setText(department.getIntroduction());
			featureTv.setText(department.getFeature());
			learningSubjectTv.setText(department.getLearningSubject());
			jobRelatedTv.setText(department.getJobRelated());
			
			pd.dismiss();
		}
	}

	public void onClick(View v) {
		switch( v.getId() ) {
		case R.id.department_intro_phone_btn :
			Uri number = Uri.parse("tel:" + phoneBtn.getText());
			Intent dial = new Intent(Intent.ACTION_DIAL, number);
			startActivity(dial);
			break;
		}
	}
}
